import { isPhone,isNumber } from "../../utils/util";
import { list } from "../../api/manager";

const reg = /^(\d{3})\d{4}(\d{4})$/;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        flag: false,
        id: null,
        query: {
            pageNum: 1,
            pageSize: 20,
            keyword: ''
        },
        params:{
            name:'',
            phone:'',

        },
        show: false,
        duration: 300,
        position: 'bottom',
        round: false,
        overlay: true,
        customerManager: [],
        copy: [
            {
                id: 1,
                name: 'liucs',
                phone: '13566768678'
            }
        ],
        
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.data.query.pageNum = 1
        this.data.pageSize = 20
        this.setData({
            customerManager: [],
            [`query.pageNum`]: this.data.query.pageNum,
            [`query.pageSize`]: this.data.query.pageSize,
        })
        this.search()
    },
    chooseCustomerManager(e){
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        prevPage.setData({
            accoManager:e.currentTarget.dataset.item.name,
            phone:e.currentTarget.dataset.item.phone,
        })
        wx.navigateBack({
            delta: 1
        })
    },
    keyword(e){
        this.data.customerManager = []
         this.data.query.keyword =  e.detail.value
         this.data.query.pageNum = 1
         this.setData({
            [`query.pageNum`]: this.data.query.pageNum,
            keyword:this.data.keyword,
            customerManager:this.data.customerManager
         })
        this.search()
    },
    onPullDownRefresh() {
        wx.showLoading({
            title: '加载中',
        })
        this.data.customerManager = []
        this.data.query.pageNum = 1
        this.data.query.keyword =  ""
        this.setData({
            [`query.keyword`]: this.data.query.keyword,
            [`query.pageNum`]: this.data.query.pageNum,
            customerManager: this.data.customerManager
        })
        this.search()
        setTimeout(() => {
            this.search(() => {
                wx.hideLoading()
                wx.stopPullDownRefresh()
            })
        }, 1000)
    },
    onReachBottom() {
        this.data.query.pageNum += 1
        this.setData({
            [`query.pageNum`]: this.data.query.pageNum
        })
        this.search()
    },

    search(callBack) {
        let params = this.data.query
        list(params).then(res=>{
            res.data.data.forEach(item =>{
                this.data.customerManager.push(item)
            })
            this.setData({
                customerManager: this.data.customerManager
            })
        })
        this.hidePhone()
        //如果回调函数存在，则执行该回调函数。
        callBack && callBack()
    },
    hidePhone() {
        for (let i = 0; i < this.data.customerManager.length; i++) {
            this.data.customerManager[i].hidePhone = this.data.customerManager[i].phone.replace(reg, '$1****$2');
        }
        this.setData({
            customerManager: this.data.customerManager
        })
    },
    onClickHidePhone(e) {
        this.data.id = e.currentTarget.dataset.id;
        this.data.id = e.currentTarget.dataset.id;
        let flag = !this.data.flag;
        this.setData({
            flag: flag,
            id: this.data.id
        })
    },
    goToDetail(e) {
        console.log(e.currentTarget.dataset.id);
        wx.navigateTo({
            url: './customerManagerDetail?id=' + e.currentTarget.dataset.id,
        })
    },
    delCustomerManager(e) {
        let id = e.currentTarget.dataset.id;
        let _this = this;
        wx.showModal({
            title: '提示',
            content: '确认删除?',
            success(res) {
                if (res.confirm) {
                    //删除接口
                    
                } else if (res.cancel) {
                    return false
                }
            
            }
        });
    },
    addCustomerManager(){
        this.setData({
            show:true
        })
    },
    phone(e){
        console.log(e.detail.value);
        this.data.params.phone = e.detail.value
    },
    clearChange(){
        this.setData({
            show:false
        })
    },
    confirmChange(){
        this.setData({
            show:false
        })
    },
})